<?php
/**
 * User: @lixu
 * Date: 2019-04-12
 * Time: 15:33
 */

ini_set('display_errors', 1); // 配置文件设置了错误不可见， 使用ini_set命令设置可见
error_reporting(-1); // 设置php错误级别 -1 为显示所有级别的错误
require_once '/data/wwwroot/g_vendor/autoload.php'; // 引入自动加载，因为项目的自动加载是在vendor里面

use Biz\model\trade\Cart as mCart;
use Biz\cache\counter\GoodsAddCart as cGoodsAddCart;
use Biz\service\esSearch\Goods as esGoods;
use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;
use Biz\module\Search\GoodsSearch;


$g = 0;
$s = 999;
$goodsNumber = [];
$userNumber  = [];

for ($i=0;$i<631;$i++) {

    $cartList = mCart::select(['goods_id', 'buy_num', 'uid'], [
        'AND' => [
            'status' => 0,
            'goods_id[>=]' => $g,
            'goods_id[<=]' => $s,
        ],
    ]);

    if($cartList) {
        // 同步商品加购数量的脚本
        foreach ($cartList as $value) {
            echo '数据组成' . $value['goods_id'];
            if (!isset($goodsNumber[$value['goods_id']])) {
                $goodsNumber[$value['goods_id']] = $value['buy_num'];
            } else {
                $goodsNumber[$value['goods_id']] = $goodsNumber[$value['goods_id']] + $value['buy_num'];
            }

            if (!isset($userNumber[$value['goods_id']][$value['uid']])) {
                $userNumber[$value['goods_id']][$value['uid']] = $value['uid'];
            }
            echo PHP_EOL;
        }

        foreach ($goodsNumber as $goodsId => $number) {
            cGoodsAddCart::incrAddCartNumber($goodsId, cGoodsAddCart::ADD_CART_TYPE_GOODS, $number);
        }

        foreach ($userNumber as $goodsId => $value) {
            echo 'user缓存查询' . $goodsId;
            echo PHP_EOL;
            cGoodsAddCart::incrAddCartNumber($goodsId, cGoodsAddCart::ADD_CART_TYPE_USER, count($value));
        }

        $goodsList = array_unique(array_column($cartList, 'goods_id'));

        foreach ($goodsList as $value) {
            echo '商品' . $value;
            echo PHP_EOL;

            $number = cGoodsAddCart::getGoodsAddCart($value);

            $info = [
                'id' => $value,
                'add_cart_num' => $number
            ];

            $res = esGoods::saveByGoodsId($info);

            if ($res['code'] != 1) {
                var_dump($res);
            }
        }
    }


$g = $g + 1000;
$s = $s + 1000;

$goodsNumber = [];
$userNumber  = [];
}


